package com.huosu.utils;

import com.baomidou.mybatisplus.extension.p6spy.StdoutLogger;
import org.springframework.stereotype.Component;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
import java.util.Vector;

/**
 * @author ：liyingchun
 * @date ：Created in 2019/7/4 0004 15:03
 * @description：时间工具类
 * @modified By：
 */
@Component
public class DateUtil {

    /**
     * 时间戳转换成日期格式字符串
     *
     * @param seconds 精确到秒的字符串
     * @param format
     * @return
     */
    public static String timeStampToDate(String seconds, String format) {
        if (seconds == null || seconds.isEmpty() || "null".equals(seconds)) {
            return "";
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds + "000")));
    }

    /**
     * 日期格式字符串转换成时间戳
     *
     * @param date_str 字符串日期
     * @param format   如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String dateToTimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime() / 1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 取得当前时间戳（精确到秒）
     *
     * @return
     */
    public static String getTimeStamp() {
        long time = System.currentTimeMillis();
        String t = String.valueOf(time / 1000);
        return t;
    }

    /**
     * 获取当前系统时间字符串
     * 格式 yyyy-MM-dd
     *
     * @return
     */
    public static String getDateStr() {
        String timeStamp = getTimeStamp();
        String date = timeStampToDate(timeStamp, "yyyy-MM-dd");
        return date;
    }

    /**
     * 获取当前系统时间字符串
     * 格式 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getTimeStr() {
        String timeStamp = getTimeStamp();
        String date = timeStampToDate(timeStamp, "yyyy-MM-dd HH:mm:ss");
        return date;
    }

    /**
     * 时间格式转字符串
     *
     * @param date
     * @return
     */
    public static String date2Str(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str = sdf.format(date);
        return str;
    }

    /**
     * 两个时间之间相差距离多少分钟
     *
     * @param str1
     * @param str2
     * @return
     * @throws Exception
     */
    public static Long getDistanceMins(String str1, String str2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date start;
        Date end;
        long days = 0;
        try {
            start = df.parse(str1);
            end = df.parse(str2);
            long time1 = start.getTime();
            long time2 = end.getTime();
            long diff = time2 - time1;
            days = diff / (1000 * 60);
        } catch (ParseException e) {
            days = 0;
        }
        return days;
    }

    /**
     * 返回运行时长显示文字
     *
     * @param startTime
     * @return
     */
    public static String getConnectViewText(Date startTime, Date nowTime) {

        if (startTime == null) {
            return "-";
        }

        long diff = nowTime.getTime() - startTime.getTime();
        long days = diff / (1000 * 60 * 60 * 24);
        long hours = (diff - days * (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
        long minutes = (diff - days * (1000 * 60 * 60 * 24) - hours * (1000 * 60 * 60)) / (1000 * 60);
        String connectTime = days + "天" + hours + "小时" + minutes + "分钟";
        if (days == 0) {
            connectTime = hours + "小时" + minutes + "分钟";
            if (hours == 0) {
                if (minutes > 0) {
                    connectTime = minutes + "分钟";
                } else {
                    connectTime = "<1分钟";
                }
            }
        }

        return connectTime;
    }

    /**
     * 返回运行时长显示文字
     *
     * @param m
     * @return
     */
    public static String getKeepTimeViewText(long m) {

        if (m == -1) {
            return "未知";
        } else if (m == 0) {
            return "1秒";
        } else if (m < 60) {//秒
            return m + "秒";
        } else if (m < 3600) {//分
            return (m / 60) + "分" + (m % 60) + "秒";
        } else if (m < 3600 * 24) {//时
            return (m / 60 / 60) + "小时" + (m / 60 % 60) + "分" + (m % 60) + "秒";
        } else if (m >= 3600 * 24) {//天
            return (m / 60 / 60 / 24) + "天" + (m / 60 / 60 % 24) + "小时" + (m / 60 % 60) + "分" + (m % 60) + "秒";
        }

        return "未知";
    }

    public static Date strToDate(String strDate) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        ParsePosition pos = new ParsePosition(0);
        Date strtodate = formatter.parse(strDate, pos);
        return strtodate;
    }

    /**
     * 获取当前系统时间字符串
     * 格式 yyyyMMddHHmmss
     *
     * @return
     */
    public static String getTimeStrWithoutFormat() {
        String timeStamp = getTimeStamp();
        String date = timeStampToDate(timeStamp, "yyyyMMddHHmmss");
        return date;
    }

    public static Date date2beijing(String gmtTime,String z){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("GMT"+z));
        Date utcDate = null;
        try {
            utcDate = sdf.parse(gmtTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        Date locatlDate = null;
        String localTime = sdf.format(utcDate.getTime());
        try {
            locatlDate = sdf.parse(localTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return locatlDate;
    }

}
